#!/bin/bash

# Get all function names for this application.
# Search for the function to ensure they are being used

cd /u/cem/cdc/web
#filePrefix="acc_"
filePrefix="$1"

NC='\033[0m' # No Color
Black='\033[0;30m'
DarkGray='\033[1;30m'
Blue='\033[0;34m'
LightBlue='\033[1;34m'
Green='\033[0;32m'
LightGreen='\033[1;32m'
Cyan='\033[0;36m'
LightCyan='\033[1;36m'
Red='\033[0;31m'
LightRed='\033[1;31m'
Purple='\033[0;35m'
LightPurple='\033[1;35m'
BrownOrange='\033[0;33m'
Yellow='\033[1;33m'
LightGray='\033[0;37m'
White='\033[1;37m'

showSearchedFiles=0

clear && printf '\033[3J' #Clear the screen
function execute {
  if [[ "$fileParam" == "" ]]; then
    #No filenames passed in, use list of files in SVN status
    echo -e "${Purple}-------------------------------------------${NC}";
    echo -e "Searching all outstanding files${NC}";
    if [[ $showSearchedFiles -eq 1 ]]; then
      echo -e "Files being searched${NC}";
      echo -e "";
    fi
    while read op fname dummy
    do
      if [[ "$fname" == *"/free/"* ]]; then
        continue; #Skip free folders
      fi
      if [[ $showSearchedFiles -eq 1 ]]; then
        echo "$fname"
      fi
      fileParam="$fileParam $fname"
    done < <(svn status)
    echo -e "${Purple}-------------------------------------------${NC}";
    grep -rn --color --exclude-dir="free" --exclude="*.swp" "TODO" $fileParam
  else
    grep -rn --color --include="*$filePrefix*" --exclude="*.swp" "TODO" *
  fi
}
function helper {
  echo "  aw_todo [File Prefix]"
  echo "  v1.1"
  echo "    [File Prefix] => Files to be included"
  echo "  eg aw_todo acc_ "
}

if [[ "$filePrefix" == "?" ]] || [[ "$filePrefix" == *"help"* ]]
 then
   helper
else
  execute $filePrefix
fi

